using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Xml;
using Microsoft.SharePoint.WebPartPages;
using PS.WebParts.Rollup;

namespace PS.WebParts
{
    [ParseChildren(ChildrenAsProperties = true), PersistChildren(false)]
    [DefaultProperty("Filters")] 
    public class BusinessDataSource : ATabularDataSource
    {
        private string strFilters;

        protected BusinessDataView DSView
        {
            get
            {
                return (BusinessDataView)base.GetView();
            }
        }

        public IEnumerable ExecuteTest()
        {
            return DSView.Test();
        }

        protected override ATabularDataSourceView CreateViewInstance()
        {
            return new BusinessDataView(base.Context, this, "");
        }

        protected override DataSourceView GetView(string viewName)
        {
            return this.DSView;
        }

        [Browsable(true), WebPartStorage(Storage.Personal), Category("RollUp"), DefaultValue(""), FriendlyName("Fields"), Description("Comma separated columns to return from BDC Entity")]
        public string Fields
        {
            get
            {
                return DSView.FieldNames;
            }
            set
            {
                DSView.FieldNames = value;
            }
        }
        [Browsable(true), WebPartStorage(Storage.Personal), Category("RollUp"), DefaultValue(""), FriendlyName("Entity Name"), Description("Entity name to query")]
        public string EntityName
        {
            get
            {
                return DSView.EntityName;
            }
            set
            {
                DSView.EntityName = value;
            }
        }
        [Browsable(true), WebPartStorage(Storage.Personal), Category("RollUp"), DefaultValue(""), FriendlyName("LoB Instance Name"), Description("LoB Instance name to query")]
        public string LobInstanceName
        {
            get
            {
                return DSView.LobInstanceName;
            }
            set
            {
                DSView.LobInstanceName = value;
            }
        }

        [DefaultValue(""), WebPartStorage(Storage.Personal), FriendlyName("BDC Filter"), Description("BDC Query Filters"), Bindable(true), Browsable(true), Category("RollUp"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), PersistenceMode(PersistenceMode.InnerProperty)]
        public string Filters
        {
            get
            {
                List<BusinessDataFilter> fc = DSView.EntityFilters;
                if (fc.Count == 0 && !string.IsNullOrEmpty(strFilters))
                    return strFilters;

                StringBuilder sb = new StringBuilder(fc.Count * 20);

                foreach (BusinessDataFilter f in fc)
                {
                    sb.AppendFormat("\n<Filter Name=\"{0}\" ", f.FilterName);
                    if (!string.IsNullOrEmpty(f.FilterValue))
                    {
                        sb.AppendFormat("Value=\"{0}\" ", f.FilterValue);
                    }

                    sb.Append("/>");
                }

                return sb.ToString();
            }
            set
            {
                strFilters = null;
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.LoadXml("<foo>" + value + "</foo>");
                    List<BusinessDataFilter> fc = DSView.EntityFilters;
                    fc.Clear();
                    foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                    {
                        if (node.LocalName != "Filter") continue;
                        XmlElement el = (XmlElement)node;

                        string name = el.GetAttribute("Name");
                        if (string.IsNullOrEmpty(name))
                            continue;
                        BusinessDataFilter f = new BusinessDataFilter(name, el.GetAttribute("Value"));

                        fc.Add(f);
                    }
                }
                catch (XmlException)
                {
                    strFilters = value;
                }
            }
        }


    }
}
